home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / chrome / sync.jar / content / preferences.js < prev    next >
Text File  |  2008-07-31  |  13KB  |  348 lines

  1. var Ci = Components.interfaces;
  2. var Cc = Components.classes;
  3. var Cr = Components.results;
  4.  
  5. function WeavePrefs() {
  6.   this._init();
  7. }
  8.  
  9. WeavePrefs.prototype = {
  10.   get _stringBundle() {
  11.     let stringBundle = document.getElementById("weaveStringBundle");
  12.     this.__defineGetter__("_stringBundle", function() { return stringBundle });
  13.     return this._stringBundle;
  14.   },
  15.  
  16.   _init : function WeavePrefs__init() {
  17.   },
  18.  
  19.   _checkClientInfo: function WeavePrefs__checkClientInfo() {
  20.     let richlistbox = document.getElementById('sync-clients-list');
  21.     let clients = Weave.ClientData.clients();
  22.     for (let guid in clients) {
  23.       let richlistitem = document.createElement('richlistitem');
  24.       let label = document.createElement('label');
  25.  
  26.       label.setAttribute("value", clients[guid].name);      
  27.  
  28.       richlistitem.appendChild(label);
  29.       richlistbox.appendChild(richlistitem);
  30.     }
  31.   },
  32.  
  33.   _checkAccountInfo: function WeavePrefs__checkAccountInfo() {
  34.     let signOnButton = document.getElementById('sync-signon-button');
  35.     let signOutButton = document.getElementById('sync-signout-button');
  36.     let syncNowButton = document.getElementById('sync-syncnow-button');
  37.     let createButton = document.getElementById('sync-create-button');
  38.     let syncUserName = document.getElementById('sync-username-field');
  39.     let changePasswordForm =
  40.       document.getElementById('sync-change-password-form');
  41.  
  42.     if (!Weave.Service.isLoggedIn) {
  43.       signOnButton.setAttribute("hidden", "false");
  44.       signOutButton.setAttribute("hidden", "true");
  45.       createButton.setAttribute("hidden", "false");
  46.       syncNowButton.setAttribute("disabled", "true");
  47.       syncUserName.setAttribute("value", "");
  48.       changePasswordForm.hidden = true;
  49.     } else {
  50.       let signedInDescription =
  51.         this._stringBundle.getFormattedString("signedIn.description",
  52.                                               [Weave.Service.username]);
  53.       signOnButton.setAttribute("hidden", "true");
  54.       signOutButton.setAttribute("hidden", "false");
  55.       createButton.setAttribute("hidden", "true");
  56.       syncNowButton.setAttribute("disabled", "false");
  57.       syncUserName.setAttribute("value", signedInDescription);
  58.       changePasswordForm.hidden = false;
  59.    }
  60.  
  61.    if(Weave.DAV.locked)
  62.      syncNowButton.setAttribute("disabled", "true");
  63.   },
  64.  
  65.   onPaneLoad: function WeavePrefs_onPaneLoad() {
  66.     this._checkAccountInfo();
  67.     this._checkClientInfo();
  68.   },
  69.  
  70.   openActivityLog: function WeavePrefs_openActivityLog() {
  71.     let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
  72.       getService(Ci.nsIWindowMediator);
  73.     let logWindow = wm.getMostRecentWindow('Weave:Log');
  74.     if (logWindow)
  75.       logWindow.focus();
  76.      else {
  77.        var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
  78.          getService(Ci.nsIWindowWatcher);
  79.        ww.openWindow(null, 'chrome://weave/content/log.xul', '',
  80.                      'chrome,centerscreen,dialog,modal,resizable=yes', null);
  81.      }
  82.   },
  83.  
  84.   doSyncNow: function WeavePrefs_doSyncNow() {
  85.     let syncNowButton = document.getElementById('sync-syncnow-button');
  86.     syncNowButton.setAttribute("disabled", "true");
  87.     let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
  88.       getService(Ci.nsIWindowMediator);
  89.     let window = wm.getMostRecentWindow("Sync:Status");
  90.     if (window)
  91.       window.focus();
  92.      else {
  93.        var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
  94.          getService(Ci.nsIWindowWatcher);
  95.        let options = 'chrome,centerscreen,dialog,modal,resizable=yes';
  96.        ww.activeWindow.openDialog("chrome://weave/content/status.xul", '', options, null);
  97.      }
  98.     syncNowButton.setAttribute("disabled", "false");
  99.   },
  100.  
  101.   openAdvancedPrefs: function WeavePrefs_openAdvancedPrefs() {
  102.          window.openDialog('chrome://weave/content/advanced.xul', '',
  103.                       'chrome, dialog, modal, resizable=yes, ', null);
  104.   },
  105.  
  106.   doSignOn: function WeavePrefs_doSignOn() {
  107.  
  108.     let branch = Cc["@mozilla.org/preferences-service;1"].
  109.       getService(Ci.nsIPrefBranch);
  110.     let username = branch.getCharPref("extensions.weave.username");
  111.  
  112.     if (!username || username == 'nobody') {
  113.          window.openDialog('chrome://weave/content/wizard.xul', '',
  114.               'chrome, dialog, modal, resizable=yes', null);
  115.     } else {
  116.          window.openDialog('chrome://weave/content/login.xul', '',
  117.                       'chrome, dialog, modal, resizable=yes', null);
  118.     }
  119.  
  120.     this.onPaneLoad();
  121.   },
  122.  
  123.   doSignOut: function WeavePrefs_doSignOut() {
  124.     Weave.Service.logout();
  125.     this._checkAccountInfo();
  126.   },
  127.  
  128.   doToggleShowPasswords: function WeavePrefs_doToggleShowPasswords(event) {
  129.     let passwordFields = ["oldPassword", "newPassword", "newPasswordAgain"].
  130.                map(function(v) document.getElementById(v));
  131.     for each (let field in passwordFields) {
  132.       if (event.target.checked)
  133.     field.removeAttribute("type");
  134.       else
  135.     field.setAttribute("type", "password");
  136.     }
  137.   },
  138.  
  139.   /**
  140.    * Make sure the new password fields contain identical values.
  141.    *
  142.    * Note: we don't validate the passwords unless both of them have been
  143.    * entered.  Otherwise it would be annoying to users who enter one and are
  144.    * told they don't match before they've had a chance to enter the other.
  145.    *
  146.    * Note: we also do this in doChangePassword before submitting the form,
  147.    * but there we display the error even if one of the values is empty, so
  148.    * we don't submit the form unless the data is valid.
  149.    */
  150.   validateNewPasswords: function WeavePrefs_validateNewPasswords() {
  151.     let newPassword = document.getElementById("newPassword");
  152.     let newPasswordAgain = document.getElementById("newPasswordAgain");
  153.  
  154.     // Don't annoy the user until they've entered something into both fields.
  155.     if (!newPassword.value || !newPasswordAgain.value)
  156.       return;
  157.  
  158.     if (newPassword.value == Weave.Service.passphrase)
  159.       this._setChangePasswordStatus("error", ["passwordSameAsPassphrase"]);
  160.     else if (newPassword.value != newPasswordAgain.value)
  161.       this._setChangePasswordStatus("error", ["passwordsDoNotMatch"]);
  162.     else
  163.       this._setChangePasswordStatus("idle");
  164.   },
  165.  
  166.   doChangePassword: function WeavePrefs_doChangePassword() {
  167.     let username = Weave.Service.username;
  168.     let oldPassword = document.getElementById("oldPassword");
  169.     let newPassword = document.getElementById("newPassword");
  170.     let newPasswordAgain = document.getElementById("newPasswordAgain");
  171.     let changePasswordButton = document.getElementById("changePasswordButton");
  172.  
  173.     // Validate the form.
  174.     let errorCodes = [];
  175.     if (!newPassword.value)
  176.       errorCodes.push("-11");
  177.     if (!oldPassword.value)
  178.       errorCodes.push("-8");
  179.     if (newPassword.value == Weave.Service.passphrase)
  180.       errorCodes.push("passwordSameAsPassphrase");
  181.     if (newPassword.value != newPasswordAgain.value)
  182.       errorCodes.push("passwordsDoNotMatch");
  183.  
  184.     if (errorCodes.length > 0) {
  185.       this._setChangePasswordStatus("error", errorCodes);
  186.       return;
  187.     }
  188.  
  189.     let url = Weave.Utils.prefs.getCharPref("serverURL") +
  190.           "api/register/chpwd/";
  191.  
  192.     let data = "uid=" + encodeURIComponent(username) +
  193.                "&password=" + encodeURIComponent(oldPassword.value) +
  194.            "&new=" + encodeURIComponent(newPassword.value);
  195.  
  196.     let self = this;
  197.     let callback = function(event) { self.onChangePassword(event) };
  198.  
  199.     this._setChangePasswordStatus("active");
  200.  
  201.     let request = new XMLHttpRequest();
  202.     request.open("POST", url, true);
  203.     request.onload = request.onerror = callback;
  204.     request.setRequestHeader("Content-Type",
  205.                  "application/x-www-form-urlencoded");
  206.     request.send(data);
  207.   },
  208.  
  209.   _setChangePasswordStatus:
  210.   function WeavePrefs__setChangePasswordStatus(status, errorCodes) {
  211.     let statusBox = document.getElementById("changePasswordStatus");
  212.     let description = document.getElementById("changePasswordDescription");
  213.  
  214.     // A list of fields in the form.  We use this array to disable the fields
  215.     // while we're in the process of changing the password, so the user doesn't
  216.     // think they can make a change once the process is underway.
  217.     let fields = ["oldPassword", "newPassword", "newPasswordAgain",
  218.           "changePasswordButton"].
  219.            map(function(v) document.getElementById(v));
  220.  
  221.     // Set the status attribute to update the icon (which gets set via CSS).
  222.     statusBox.setAttribute("status", status);
  223.  
  224.     if (status == "active") {
  225.       // Disable the form fields so the user doesn't think they can change
  226.       // them in the middle of the process.
  227.       for each (let field in fields)
  228.     field.disabled = true;
  229.  
  230.       description.value =
  231.     this._stringBundle.getString("change.password.status.active");
  232.     }
  233.     else {
  234.       // Reenable the form fields so the user can use the form (again).
  235.       for each (let field in fields)
  236.     field.disabled = false;
  237.  
  238.       switch(status) {
  239.     case "success":
  240.       description.value =
  241.         this._stringBundle.getString("change.password.status.success");
  242.       break;
  243.  
  244.     case "error":
  245.       let errorProperty;
  246.  
  247.       // We only have space to show one error message, so we check the codes
  248.       // in order of importance and show the most important one.
  249.       if (errorCodes.indexOf("passwordSameAsPassphrase") != -1)
  250.             errorProperty = "change.password.status.passwordSameAsPassphrase";
  251.           else if (errorCodes.indexOf("passwordsDoNotMatch") != -1)
  252.         errorProperty = "change.password.status.passwordsDoNotMatch";
  253.       else if (errorCodes.indexOf("-12") != -1)
  254.         errorProperty = "change.password.status.badOldPassword";
  255.       else if (errorCodes.indexOf("-11") != -1)
  256.         errorProperty = "change.password.status.noNewPassword";
  257.       else if (errorCodes.indexOf("-8") != -1)
  258.         errorProperty = "change.password.status.noOldPassword";
  259.       else
  260.         errorProperty = "change.password.status.error";
  261.  
  262.       description.value = this._stringBundle.getString(errorProperty);
  263.  
  264.       break;
  265.  
  266.     case "idle":
  267.     default:
  268.       description.value = description.getAttribute("idlevalue");
  269.       break;
  270.       }
  271.     }
  272.   },
  273.  
  274.   onChangePassword: function WeavePrefs_onChangePassword(event) {
  275.     let request = event.target;
  276.  
  277.     switch(request.status) {
  278.       case 200:
  279.     this._setChangePasswordStatus("success");
  280.     Weave.Service.password = document.getElementById("newPassword").value;
  281.     break;
  282.       default:
  283.     this._setChangePasswordStatus("error", request.responseText);
  284.     break;
  285.     }
  286.   },
  287.  
  288.   doCreateAccount: function WeavePrefs_doCreateAccount() {
  289.     window.openDialog('chrome://weave/content/wizard.xul', '',
  290.                       'chrome,centerscreen,dialog,resizable=yes', null);
  291.   },
  292.  
  293.   resetLoginCredentials: function WeavePrefs_resetLoginCredentials() {
  294.     let p = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  295.       .getService(Ci.nsIPromptService);
  296.     if (p.confirm(null,
  297.                   this._stringBundle.getString("reset.login.warning.title"),
  298.                   this._stringBundle.getString("reset.login.warning"))) {
  299.       Weave.Service.logout();
  300.       Weave.Service.password = null;
  301.       Weave.Service.passphrase = null;
  302.       Weave.Service.username = null;
  303.       this._checkAccountInfo();
  304.       this._checkClientInfo();
  305.     }
  306.   },
  307.  
  308.   resetServerURL: function WeavePrefs_resetServerURL() {
  309.     let branch = Cc["@mozilla.org/preferences-service;1"].
  310.       getService(Ci.nsIPrefBranch);
  311.     branch.clearUserPref("extensions.weave.serverURL");
  312.     let serverURL = branch.getCharPref("extensions.weave.serverURL");
  313.     let serverField = document.getElementById('sync-server-field');
  314.     serverField.setAttribute("value", serverURL);
  315.     Weave.Service.logout();
  316.   },
  317.  
  318.   resetLock: function WeavePrefs_resetLock() {
  319.     let p = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  320.       .getService(Ci.nsIPromptService);
  321.     if (p.confirm(null,
  322.                   this._stringBundle.getString("reset.lock.warning.title"),
  323.                   this._stringBundle.getString("reset.lock.warning"))) {
  324.        Weave.Service.resetLock();
  325.     }
  326.   },
  327.  
  328.   resetServer: function WeavePrefs_resetServer() {
  329.     let p = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  330.       .getService(Ci.nsIPromptService);
  331.     if (p.confirm(null,
  332.                   this._stringBundle.getString("reset.server.warning.title"),
  333.                   this._stringBundle.getString("reset.server.warning")))
  334.       Weave.Service.serverWipe();
  335.   },
  336.  
  337.   resetClient: function WeavePrefs_resetClient() {
  338.     let p = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  339.       .getService(Ci.nsIPromptService);
  340.     if (p.confirm(null,
  341.                   this._stringBundle.getString("reset.client.warning.title"),
  342.                   this._stringBundle.getString("reset.client.warning")))
  343.       Weave.Service.resetClient();
  344.   }
  345. };
  346.  
  347. let gWeavePrefs = new WeavePrefs();
  348.